var tableCombo = new Ext.form.ComboBox({
	id : 'table-combo',
	name : 'table-combo',
	store : new Ext.data.SimpleStore({
		fields : [ 'name' ],
		data : [ [ 'domainmodel' ],
		         [ 'domainmodelattr' ],
				 [ 'domainmodelstate' ],
				 [ 'epcunit' ],
				 [ 'epcunits' ],
				 [ 'fileinfo' ],
				 [ 'functions' ],
				 [ 'parameter' ],
				 [ 'permission' ],
				 [ 'permisssionresources' ],
				 [ 'process' ],
				 [ 'resource' ],
				 [ 'role' ],
				 [ 'rolepermissions' ],
				 [ 'subprocess' ],
				 ]
	}),
	valueField : "name",
	displayField : "name",
	editable : false,
	forceSelection : true,
	typeAhead : true,
	triggerAction : 'all',
	selectOnFocus : true,// 用户不能自己输入,只能选择列表中有的记录
	allowBlank : false,
	mode : 'local',
	listeners : {
		select : function() {
			var tn=tableCombo.getValue();
			columnStore.load({ params: { tablename: tn} });
		},
		afterRender : function(combo) {
			tableCombo.setValue('domainmodel');//同时下拉框会将与name为firstValue值对应的 text显示
		}
	}
});

var columnStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showTableColumn.action?method=showTableColumn',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "column",
		totalProperty : "totalColumnList",
		id : "columnname"
	}, [ {
		name : 'id'
	}, {
		name : 'columnname'
	}, {
		name : 'tablename'
	}, {
		name : 'type'
	} ])
});

var columnModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer(),
		{
			header : '表名',
			width : 100,
			dataIndex : 'tablename',
			sortable : true,
			id : 'tablename'
		},
		{
			header : '字段名',
			width : 120,
			dataIndex : 'columnname',
			sortable : true,
			align : 'columnname'
		},
		{
			header : '字段类型',
			width : 60,
			dataIndex : 'type',
			sortable : true,
			align : 'center',
			renderer : function(value, cellmeta, record) {
				var typemap = [ [ '0', 'int' ], [ '1', 'float' ],
						[ '2', 'bool' ], [ '3', 'varchar' ] ];
				return typemap[value][1];
			}
		}]
);

var columnList = new Ext.grid.GridPanel({
	enableColumnResize : true,
	border : true,
	clicksToEdit : 1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows : true, //显示斑马线
	loadMask : true,
	autoHeight : true,
	bodyStyle : 'width:100%',
	autoWidth : true,
	editable : true,
	cm : columnModel,
	store : columnStore,
	tbar:[
	      '选择数据表：',
	      tableCombo,
	      '|',
	      '字段名:',	      
	      new Ext.form.TextField({
	    		id:'columnname',
	    		name:'columname',
	    		width:100,
	    		allowblank:false,
	    		enableKeyEvents:true
	      }),
	      '字段类型:',	      
	      new Ext.form.ComboBox( {
	    	    id :"columntype",
	    	    name:'columntype',
	    	    allowblank:true,
	    	    fieldLabel:'类型',
	    		emptyText: '请选择',
	    		mode: 'local',  
	    		width:100,
	    	    triggerAction: 'all',   
	    	    valueField: 'id',   
	    	    displayField: 'text',   
	    	    editable:false,  
	    	    selectOnFocus : false,  
	    	    forceSelection : false,
	    		store: new Ext.data.SimpleStore({   
	    	        fields: ['id','text'],
	    	        data: [ [ '0', 'int' ], [ '1', 'float' ],
							[ '2', 'bool' ], [ '3', 'varchar' ] ]   
	    	    })
	      }),
	      {
		  		xtype:'button',
		  		iconCls : 'search',
		  		width:60,
		  		text:'添加字段',
		  		handler:function(){
		  			var tablename=tableCombo.getValue();
		  			var columnname=Ext.getCmp('columnname').getValue();
		  			var columntype=Ext.getCmp('columntype').getValue();
		  			if(columnname==null || columnname==''){
		  				Ext.Msg.alert('消息','请填写字段名');
		  				return;
		  			}
		  			if(columntype==null || columntype==''){
		  				Ext.Msg.alert('消息','请选择字段类型');
		  				return;
		  			}
		  			var re=/^[a-zA-Z0-9_]{3,32}$/;
		  			if(!re.test(columnname)){
		  				Ext.Msg.alert('消息','字段名只能由字母数字组成，长度为3-32位。');
		  				return;
		  			}
		  			Ext.Ajax.request({
		  				url: 'addTableColumn.action?method=addTableColumn',	
		  				params:{
		  					tablename:tablename,
		  					columnname:columnname,
		  					type:columntype
		  				},
		  			   	success: function(res,opt){
		  			   		Ext.Msg.alert('消息','添加成功！');
		  			   		Ext.getCmp('columnname').setValue('');
			  				Ext.getCmp('columntype').setValue('');
		  			   		columnStore.load({ params: { tablename: tablename} });
		  			   	},
		  			   	failure: function(){
		  			   		
		  			   	}
		  			});
		  		}
		  }
	]
});

var tablemgr = new Ext.Panel({
	title : '数据字段管理',
	id : 'table-manager-panel',
	border : 'fit',
	items : [
	          columnList],
	showPage : function() {
		var tn=tableCombo.getValue();
		columnStore.load({ params: { tablename: tn} });
	}
});